import { routerRedux } from 'dva/router';
import * as menuService from '../services/system/MenuService';

import { getMenuData } from '../common/menu';

export default {
  namespace: 'loginMenu',

  state: {
    loading:false,
    menuData:[],
  },

  effects: {
    *fetchMenu(_, { call, put }) {
      const response = yield call(menuService.getMenu);
      if(response.code!=200){
        yield put(routerRedux.push('/user/login'));
      }

      const resTemp = getMenuData(response.result);
      yield put({
        type: 'submitMenu',
        payload: resTemp,
      });
    },
  },

  reducers: {
    submitMenu(state, { payload }) {
      return {
        ...state,
        loading:true,
        menuData: payload,
      };
    },
  },
};
